package bean.managed;

import entity.CustomerEntity;
import bean.managed.util.JsfUtil;
import bean.session.CustomerEntityFacade;

import bean.session.DepartmentEntityFacade;
import bean.session.RoleEntityFacade;
import bean.session.SalesLeadEntityFacade;
import entity.ContactEntity;
import entity.ConvertedCustomerEntity;
import entity.CustomerAccountEntity;
import entity.RightEntity;
import entity.RoleEntity;
import entity.SalesInquiryEntity;
import entity.SalesLeadEntity;
import entity.SalesQuotationEntity;
import entity.SystemUserEntity;
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import managedbean.AbstractBacking;

import type.CustomerType;
import util.Fixtures.Department;
import util.Fixtures.Role;
import util.PasswordEncryptor;
import util.SecureRandomNumberGenerator;

@ManagedBean(name = "customerEntityController")
@SessionScoped
public class CustomerEntityController extends AbstractBacking implements Serializable {

    @EJB
    private bean.session.CustomerEntityFacade ejbFacade;
    @EJB
    private RoleEntityFacade roleFacade;
    @EJB
    private DepartmentEntityFacade departmentFacade;
    
    @ManagedProperty(value = "#{salesInquiryEntityController}")
    private SalesInquiryEntityController salesInquiryEntityController;
    @ManagedProperty(value = "#{salesQuotationEntityController}")
    private SalesQuotationEntityController salesQuotationEntityController;
    @ManagedProperty(value = "#{systemMessageController}")
    private SystemMessageController systemMessageController;
    @ManagedProperty(value = "#{salesLeadEntityController}")
    private SalesLeadEntityController salesLeadEntityController;
    @ManagedProperty(value = "#{contactEntityController}")
    private ContactEntityController contactEntityController;
    @ManagedProperty(value = "#{convertedCustomerEntityController}")
    private ConvertedCustomerEntityController convertedCustomerEntityController;
    private int selectedItemIndex;
    private DataModel searchItems = null;
    private String searchField = "firstName";
    private String userSearchInput;
    private String contactNumber;
    private String email;
    //private SalesLeadEntityFacade salesLeadFacade;
    private DataModel noAccountCustomersItems = null;
    @ManagedProperty(value = "#{purchaseOrderEntityController}")
    private PurchaseOrderEntityController purchaseOrderEntityController;
    //private CustomerEntity prepareSelect;
    private SalesLeadEntity currentSalesLead;
    private CustomerEntity current = new CustomerEntity();
    private DataModel items = null;
    private DataModel allCustomersItem = null;
    private DataModel executiveCustomersItem = null;
    private CustomerEntity customer;
    private SalesInquiryEntity salesInquiry;
    private SalesQuotationEntity salesQuotation;
    private ContactEntity contactHolder;
    @ManagedProperty(value = "#{salesOrderEntityController}")
    private SalesOrderEntityController salesOrderEntityController;

    public ConvertedCustomerEntityController getConvertedCustomerEntityController() {
        return convertedCustomerEntityController;
    }

    public void setConvertedCustomerEntityController(ConvertedCustomerEntityController convertedCustomerEntityController) {
        this.convertedCustomerEntityController = convertedCustomerEntityController;
    }

    public CustomerEntityController() {
    }

    public ContactEntityController getContactEntityController() {
        return contactEntityController;
    }

    public void setContactEntityController(ContactEntityController contactEntityController) {
        this.contactEntityController = contactEntityController;
    }

    public SalesInquiryEntity getSalesInquiry() {
        return salesInquiry;
    }

    public void setSalesInquiry(SalesInquiryEntity salesInquiry) {
        this.salesInquiry = salesInquiry;
    }

    public SalesQuotationEntity getSalesQuotation() {
        return salesQuotation;
    }

    public void setSalesQuotation(SalesQuotationEntity salesQuotation) {
        this.salesQuotation = salesQuotation;
    }

    public CustomerEntity getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerEntity customer) {
        this.customer = customer;
    }

    public SystemMessageController getSystemMessageController() {
        return systemMessageController;
    }

    public void setSystemMessageController(SystemMessageController systemMessageController) {
        this.systemMessageController = systemMessageController;
    }

    public String prepareAccount() {
//        current = (CustomerEntity) getNoAccountCustomersItems().getRowData();
        //selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        //prepareList();
        return "Create?faces-redirect=true";
    }

    public DataModel getNoAccountCustomersItems() {
        //if (noAccountCustomersItems == null) {
        noAccountCustomersItems = ejbFacade.findNoAccountCustomers();
        //}
        return noAccountCustomersItems;
    }

    public void setNoAccountCustomersItems(DataModel noAccountCustomersItems) {
        this.noAccountCustomersItems = noAccountCustomersItems;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public SalesLeadEntity getCurrentSalesLead() {
        return currentSalesLead;
    }

    public void setCurrentSalesLead(SalesLeadEntity currentSalesLead) {
        this.currentSalesLead = currentSalesLead;
    }

    public SalesLeadEntityController getSalesLeadEntityController() {
        return salesLeadEntityController;
    }

    public void setSalesLeadEntityController(SalesLeadEntityController salesLeadEntityController) {
        this.salesLeadEntityController = salesLeadEntityController;
    }

    public CustomerEntityFacade getEjbFacade() {
        return ejbFacade;
    }

    public void setEjbFacade(CustomerEntityFacade ejbFacade) {
        this.ejbFacade = ejbFacade;
    }

    public String getUserSearchInput() {
        return userSearchInput;
    }

    public void setUserSearchInput(String userSearchInput) {
        this.userSearchInput = userSearchInput;
    }

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public DataModel getSearchItems() {
        if (searchItems == null) {
            searchItems = ejbFacade.findCustomer(userSearchInput, searchField);
        }
        return searchItems;
    }

    public CustomerEntity getCurrent() {
        return current;
    }

    public void setCurrent(CustomerEntity current) {
        this.current = current;
        setupCurrentContact();
    }

    public CustomerEntity getSelected() {
        if (current == null) {
            current = new CustomerEntity();
            selectedItemIndex = -1;
        }
        return current;
    }

    private CustomerEntityFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {

        Iterator itr = getCurrentUser().getRights().iterator();
        while (itr.hasNext()) {
            RightEntity rightEntity = (RightEntity) itr.next();
            if (rightEntity.getRightName().equals("View All Customer")) {
                recreateModel();
                return "/entities/customerEntity/List?faces-redirect=true";
            }
        }
        Iterator roleItr = getCurrentUser().getRoles().iterator();
        while (roleItr.hasNext()) {
            RoleEntity roleEntity = (RoleEntity) roleItr.next();
            Iterator rightItr = roleEntity.getRights().iterator();
            while (rightItr.hasNext()) {
                RightEntity rightEntity = (RightEntity) rightItr.next();
                if (rightEntity.getRightName().equals("View All Customer")) {
                    recreateModel();
                    return "/entities/customerEntity/List?faces-redirect=true";
                }
            }
        }
        recreateModel();
        return "/entities/customerEntity/CustomerExecutiveCustomersList?faces-redirect=true";
    }

    private void setupCurrentContact() {
        contactEntityController.setupCurrent(current.getContact());
    }

    public String prepareView() {
        current = (CustomerEntity) getItems().getRowData();
        setupCurrentContact();
        return "View?faces-redirect=true";
    }

    public String prepareAccountPageView() {
        current = (CustomerEntity) getNoAccountCustomersItems().getRowData();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }

    public DataModel getAllCustomersItem() {
        if (allCustomersItem == null) {
            allCustomersItem = ejbFacade.findAllCustomers();
        }
        return allCustomersItem;
    }

    public String prepareSearchView() {
        current = (CustomerEntity) getSearchItems().getRowData();
       setupCurrentContact();
        return "View?faces-redirect=true";
    }

    public String prepareCreate() {
        current = new CustomerEntity();
        selectedItemIndex = -1;
        //setupCurrentContact();
        return "/entities/customerEntity/Create?faces-redirect=true";
    }

    public String createConfirm() {
        systemMessageController.getEjbFacade().createAndSendSystemMessage(current.getCustomerExecutive(), "Customer has been assigned to you", "index", systemMessageController);
        return prepareList();
    }

    public String create() {
        try {

            ContactEntity contact = current.getContact();
            contact.setContactNumber(contactNumber);
            contact.setEmail(email);
            contact.setPerson(current);
            current.setContact(contact);
            contactHolder = current.getContact();

            current.created(getLoginPerson());
            encryptPasswordForNew();
            getFacade().create(current);

            SystemUserEntity accountsReceivableManager = departmentFacade.findDepartmentByName(Department.AccountsReceivable.getName()).getManager();
              systemMessageController.getEjbFacade().createAndSendSystemMessage(accountsReceivableManager, 
                    "Customer " + customer.getFullname() + " needs to be assigned an account.", 
                    "/entities/customerAccountEntity/NoAccountCustomersList.xhtml", systemMessageController);
            postCreateRoleAssignment();

            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("CustomerEntityCreated"));
            return prepareAddress();
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    //role rights assignement
    private void postCreateRoleAssignment() {
        try {
            RoleEntity currentCustomer = roleFacade.findRole(Role.Customer.getName());
            current.getRoles().add(currentCustomer);
            currentCustomer.getUsers().add(current);
            roleFacade.edit(currentCustomer);
            ejbFacade.edit(current);
        } catch (Exception ex) {
            JsfUtil.addErrorMessage(ex, "Error setting createdBy and modifiedBy");
        }
    }

    private void encryptPasswordForNew() {
        current.setPassword(PasswordEncryptor.encryptPassword(SecureRandomNumberGenerator.nextSecureRandomNumber()));
    }

    private void resetContactAndAddresses() {
        contactEntityController.setupCurrent(contactHolder);
    }

    public String prepareAddress() {
        resetContactAndAddresses();
        return "/entities/customerEntity/CreateAddress?faces-redirect=true";
    }

    public String prepareEdit() {
        current = (CustomerEntity) getItems().getRowData();
        setupCurrentContact();
        return "Edit";
    }

    public String prepareAccountPageEdit() {
        current = (CustomerEntity) getNoAccountCustomersItems().getRowData();
        setupCurrentContact();
        return "/entities/customerEntity/Edit?faces-redirect=true";
    }

    public String update() {
        try {
            current.modified(getLoginPerson());
            getFacade().edit(current);
            prepareList();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("CustomerEntityUpdated"));
            return "/entities/customerEntity/View?faces-redirect=true";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        current = (CustomerEntity) getItems().getRowData();
        performDestroy();
        recreateModel();
        return "List?faces-redirect=true";
    }

    public String accountPageDestroy() {
        current = (CustomerEntity) getNoAccountCustomersItems().getRowData();
        performDestroy();
        recreateModel();
        return "List?faces-redirect=true";
    }

    public String destroyAndView() {
        performDestroy();
        recreateModel();
        update();
        return "/entities/customerEntity/List?faces-redirect=true";
//        if (selectedItemIndex >= 0) {
//            return "View";
//        } else {
//            // all items were removed - go back to list
//            recreateModel();
//            return "List";
//        }
    }

    public String activate() {
        current = (CustomerEntity) getItems().getRowData();
       performActivate();
        recreateModel();
        return "List?faces-redirect=true";
    }

    public String accountPageActivate() {
        current = (CustomerEntity) getNoAccountCustomersItems().getRowData();
        performActivate();
        recreateModel();
        return "List?faces-redirect=true";
    }

    private void performActivate() {
        try {
            current.setArchived(false);
            current.modified(getLoginPerson());
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SalesLeadEntityActivated"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    private void performDestroy() {
        try {
            current.setArchived(true);
            current.modified(getLoginPerson());
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("CustomerEntityDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public DataModel getItems() {
        if (items == null) {
            items = new ListDataModel(getFacade().findAll());
        }
        return items;
    }

    public DataModel getExecutiveCustomersItem() {
        //if (executiveCustomersItem == null) {
        executiveCustomersItem = getFacade().findAllExecutiveCustomers(getCurrentUser().getId());
        //}
        return executiveCustomersItem;
    }

    public void recreateModel() {
        searchItems = null;
        items = null;
        noAccountCustomersItems = null;
        executiveCustomersItem = null;
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

//     public SelectItem[] getItemsAvailableSelectOneAddCustomerAccount() {
//        return JsfUtil.getSelectItems(ejbFacade.findNoAccountCustomers(), true);
//    }
    public String prepareSearch() {
        recreateModel();
        searchItems = getSearchItems();
        return "SearchList?faces-redirect=true";
    }

    public SalesInquiryEntityController getSalesInquiryEntityController() {
        return salesInquiryEntityController;
    }

    public void setSalesInquiryEntityController(SalesInquiryEntityController salesInquiryEntityController) {
        this.salesInquiryEntityController = salesInquiryEntityController;
        this.salesInquiryEntityController.setCustomerEntityController(this);
    }

    public SalesQuotationEntityController getSalesQuotationEntityController() {
        return salesQuotationEntityController;
    }

    public void setSalesQuotationEntityController(SalesQuotationEntityController salesQuotationEntityController) {
        this.salesQuotationEntityController = salesQuotationEntityController;
        this.salesQuotationEntityController.setCustomerEntityController(this);
    }

    public PurchaseOrderEntityController getPurchaseOrderEntityController() {
        return purchaseOrderEntityController;
    }

    public void setPurchaseOrderEntityController(PurchaseOrderEntityController purchaseOrderEntityController) {
        this.purchaseOrderEntityController = purchaseOrderEntityController;
        this.purchaseOrderEntityController.setCustomerEntityController(this);
    }

    public SalesOrderEntityController getSalesOrderEntityController() {
        return salesOrderEntityController;
    }

    public void setSalesOrderEntityController(SalesOrderEntityController salesOrderEntityController) {
        this.salesOrderEntityController = salesOrderEntityController;
        this.salesOrderEntityController.setCustomerEntityController(this);
    }

    @FacesConverter(forClass = CustomerEntity.class)
    public static class CustomerEntityControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            CustomerEntityController controller = (CustomerEntityController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "customerEntityController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Long getKey(String value) {
            java.lang.Long key;
            key = Long.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Long value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof CustomerEntity) {
                CustomerEntity o = (CustomerEntity) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CustomerEntityController.class.getName());
            }
        }
    }

    public String convertToCustomer() {

        performConvert();
        salesLeadEntityController.prepareList();

        return prepareList();

    }

    public CustomerEntity purchaseOrderConvert() {
        try {

            CustomerEntity customer = new CustomerEntity();
            CustomerAccountEntity customerAccount = new CustomerAccountEntity();
            currentSalesLead = salesLeadEntityController.getCurrent();

            customer.setCustomerAccount(customerAccount);


            customer.setCompanyName(currentSalesLead.getCompanyName());
            customer.setPassword(currentSalesLead.getPassword());
            customer.setJobTitle(currentSalesLead.getJobTitle());
            customer.setFirstName(currentSalesLead.getFirstName());
            customer.setLastName(currentSalesLead.getLastName());

            customer.setCustomerType(currentSalesLead.getCustomerType());
            customer.setName(currentSalesLead.getName());
            customer.setTitle(currentSalesLead.getTitle());
            customer.setCustomerExecutive(purchaseOrderEntityController.getSalesExecutive());
            systemMessageController.getEjbFacade().createAndSendSystemMessage(purchaseOrderEntityController.getSalesExecutive(), "Customer has been assigned to you", "/index", systemMessageController);

            //send message to accountsReceivableManager
            SystemUserEntity accountsReceivableManager = departmentFacade.findDepartmentByName(Department.AccountsReceivable.getName()).getManager();
            systemMessageController.getEjbFacade().createAndSendSystemMessage(accountsReceivableManager, 
                    "Customer " + customer.getFullname() + " needs to be assigned an account.", 
                    "/entities/customerAccountEntity/NoAccountCustomersList.xhtml", systemMessageController);
            
            customer.setSalesInquiries(currentSalesLead.getSalesInquiries());
            customer.setSalesQuotations(currentSalesLead.getSalesQuotations());

            customer.created(getLoginPerson());

            fixSalesQuotations(customer);

            salesLeadEntityController.performDestroy();

            //repoint contact from currentCustomer to currentCustomer
            customer.setContact(currentSalesLead.getContact());
            customer.getContact().setPerson(customer);


            prepareList();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SalesLeadEntityConverted"));
            return customer;
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    private void performConvert() {
        try {
            CustomerEntity customer = new CustomerEntity();
            currentSalesLead = salesLeadEntityController.getCurrent();

            customer.setCompanyName(currentSalesLead.getCompanyName());
            customer.setPassword(currentSalesLead.getPassword());
            customer.setJobTitle(currentSalesLead.getJobTitle());
            customer.setFirstName(currentSalesLead.getFirstName());
            customer.setLastName(currentSalesLead.getLastName());

            customer.setCustomerType(currentSalesLead.getCustomerType());
            customer.setName(currentSalesLead.getName());
            customer.setTitle(currentSalesLead.getTitle());
            customer.setCustomerExecutive(current.getCustomerExecutive());

            customer.setSalesInquiries(currentSalesLead.getSalesInquiries());
            customer.setSalesQuotations(currentSalesLead.getSalesQuotations());

            customer.created(getLoginPerson());
            getFacade().create(customer);
            getFacade().flush();

            fixSalesInquiries(customer);

            salesLeadEntityController.performDestroy();

            //repoint contact from currentCustomer to currentCustomer
            customer.setContact(currentSalesLead.getContact());
            customer.getContact().setPerson(customer);
            getFacade().edit(customer);
            
            //send message to customer executive
            systemMessageController.getEjbFacade().createAndSendSystemMessage(customer.getCustomerExecutive(), 
                    "Customer " + customer.getFullname() + " has been assigned to you.", "/entities/customerEntity/CustomerExecutiveCustomersList.xhtml", systemMessageController);
            //send message to accountsReceivableManager
            SystemUserEntity accountsReceivableManager = departmentFacade.findDepartmentByName(Department.AccountsReceivable.getName()).getManager();
            systemMessageController.getEjbFacade().createAndSendSystemMessage(accountsReceivableManager, 
                    "Customer " + customer.getFullname() + " needs to be assigned an account.", 
                    "/entities/customerAccountEntity/NoAccountCustomersList.xhtml", systemMessageController);
            
            prepareList();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SalesLeadEntityConverted"));
                      
            // update the ConvertedCustomerEntity table upon
            ConvertedCustomerEntity convertedCustomer = new ConvertedCustomerEntity();
            convertedCustomer.setCustomer(customer);
            convertedCustomer.setConvertedDate(new Date());

            convertedCustomerEntityController.setSelected(convertedCustomer);
            convertedCustomerEntityController.create();          

        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    private void fixSalesInquiries(CustomerEntity customer) {
        if (currentSalesLead.getSalesInquiries().size() > 0) {
            Iterator itr = currentSalesLead.getSalesInquiries().iterator();
            while (itr.hasNext()) {
                salesInquiryEntityController.setCurrent((SalesInquiryEntity) itr.next());
                salesInquiryEntityController.getCurrent().setCustomer(customer);
                salesInquiryEntityController.getCurrent().setSalesLead(null);
                salesInquiryEntityController.update();
            }
        }

        System.out.println(currentSalesLead.getSalesQuotations().size());
        if (currentSalesLead.getSalesQuotations().size() > 0) {
            Iterator itr2 = currentSalesLead.getSalesQuotations().iterator();
            while (itr2.hasNext()) {
                salesQuotationEntityController.setCurrent((SalesQuotationEntity) itr2.next());
                salesQuotationEntityController.getCurrent().setCustomer(customer);
                salesQuotationEntityController.getCurrent().setSalesLead(null);
                salesQuotationEntityController.update();
            }
        }

        getFacade().edit(customer);
        getFacade().flush();
    }

    private void fixSalesQuotations(CustomerEntity customer) {

        if (currentSalesLead.getSalesQuotations().size() > 0) {
            Iterator itr2 = currentSalesLead.getSalesQuotations().iterator();
            while (itr2.hasNext()) {
                salesQuotationEntityController.setCurrent((SalesQuotationEntity) itr2.next());
                salesQuotationEntityController.getCurrent().setCustomer(customer);
                salesQuotationEntityController.getCurrent().setSalesLead(null);
                //salesQuotationEntityController.update();
            }
        }

//        getFacade().edit(customer);
//        getFacade().flush();
    }

    public CustomerType[] getCustomerType() {
        return CustomerType.values();
    }

    public String viewCreateCustomerFromInquiry() {
        current = salesInquiryEntityController.getViewCurrent().getCustomer();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }

    public String viewCustomerFromInquiry() {
        current = salesInquiryEntityController.getSelected().getCustomer();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }

    public String viewCustomerFromQuotation() {
        current = salesQuotationEntityController.getSelected().getCustomer();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }

    public String viewCustomerFromInquiryDialog() {
        current = salesInquiry.getCustomer();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }

    public String viewCustomerFromQuotationDialog() {
        current = salesQuotation.getCustomer();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }

    public String viewCustomerFromPO() {
        current = purchaseOrderEntityController.getSelected().getCustomer();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }

    public String viewCustomerFromSO() {
        current = getSalesOrderEntityController().getSelected().getCustomer();
        setupCurrentContact();
        return "/entities/customerEntity/View?faces-redirect=true";
    }
}
